package com.WISSC.serviceImpl;

import com.WISSC.bean.Authorization;
import com.WISSC.bean.AuthorizationImage;
import com.WISSC.bean.Massage;
import com.WISSC.bean.OccupationImage;
import com.WISSC.mapper.AuthorizationImageMapper;
import com.WISSC.service.AuthorizationImageService;
import com.WISSC.util.file.FileUpload;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.FileNotFoundException;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 授权机构和图片关联表 服务实现类
 * </p>
 *
 * @author 张凯
 * @since 2019-05-08
 */
@Service
public class AuthorizationImageServiceImpl extends ServiceImpl<AuthorizationImageMapper, AuthorizationImage> implements AuthorizationImageService {

    /**
     * @param file
     * @return 添加证书图片
     */
    @Override
    public Massage upload(MultipartFile file) {
        String url = "";
        try {
            url = FileUpload.fileUpload(file);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
        AuthorizationImage authorizationImage = new AuthorizationImage();
        authorizationImage.setUrl(url);
        authorizationImage.setCreateTime(new Date().getTime());
        Massage massage = new Massage();
        //上传成功 返回数据 返回本次上传的文件ID
        if (this.save(authorizationImage)) {
            massage.setCode(0);
            massage.setMsg("上传成功");
            Map map = new HashMap<>();
            map.put("fileId", authorizationImage.getId());
            map.put("url", url);
            massage.setData(map);
            return massage;
        }
        massage.setCode(1);
        massage.setMsg("上传失败");
        return massage;
    }
    /**
     *
     * @param grantId
     * @return 机构图片回显--url
     */
    @Override
    public List<AuthorizationImage> getImageUrl(Long grantId) {
        return baseMapper.getImageUrl(grantId);
    }

    /**
     *
     * @param grantId
     * @param file
     * @return 机构多图片编辑
     */
    @Override
    public Massage updatePicture(String grantId, MultipartFile file) {
        Long id = Long.parseLong(grantId);
        int i = baseMapper.remove(id);
        String url = "";
        try {
            url = FileUpload.fileUpload(file);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
        AuthorizationImage authorizationImage = new AuthorizationImage();
        authorizationImage.setUrl(url);
        authorizationImage.setGrantId(Long.parseLong(grantId));
        authorizationImage.setCreateTime(new Date().getTime());
        Massage massage = new Massage();
        if (this.save(authorizationImage)){
            massage.setCode(0);
            massage.setMsg("上传成功");
            return massage;
        }
        massage.setCode(1);
        massage.setMsg("上传失败");
        return massage;
    }

    @Override
    public void deleteByIdGrand(Long id) {
        baseMapper.deleteByIdGrand(id);
    }

    @Override
    public boolean removeByAuthorizationId(Long id) {
        if (baseMapper.remove(id)>0){
            return true;
        }
        return false;
    }

    @Override
    public List<AuthorizationImage> getByGrantId(Long id) {
        return baseMapper.getByGrantId(id);
    }

}
